package com.group3.admin.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.group3.admin.listener.ContextListener;

/**
 * Servlet implementation class PollServlet
 * 
 * 用于给浏览器；轮询mlist最新状态用的
 */
public class PollServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PollServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		String isNewest = "";
		long lastReadMListTime = 0;
		if (request.getSession().getAttribute("readMlistTime") != null) {
			lastReadMListTime = ((Date)request.getSession().getAttribute("readMlistTime")).getTime();
		}
		
		if ( lastReadMListTime < ContextListener.newestDate.getTime()) {
			System.out.println("PollServlet : the Mlist is updated! ");	// 返回
			isNewest = "notNewest";
		} else {			
//			System.out.println("PollServlet : the Mlist is newest!");
			isNewest ="newest";
		}
		response.setContentType("application/json;charset=utf-8");
		String jsonString = JSON.toJSONString(isNewest, true);
		response.getWriter().append(jsonString);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
